package com.cgtong.android.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cgtong.android.R;
import com.cgtong.android.adapter.DraftAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.callback.ErrorCallback;
import com.cgtong.android.mvp.contract.ProblemPageContract;
import com.cgtong.android.mvp.presenter.ProblemPagePresenter;
import com.cgtong.android.util.PostUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.cgtong.android.activity.ReportProblemActivity.KEY_CODE_FOR_REQUEST;
import static com.cgtong.android.activity.ReportProblemActivity.KEY_COMMIT_DRAFT;
import static com.cgtong.android.activity.ReportProblemActivity.KEY_ITEM_POS;


/**
 * 我的草稿箱
 * by zhuqingchun
 */
public class MyDraftActivity extends BaseTitleActivity implements ProblemPageContract.View {


    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;

    private List<ProblemBean> mList;
    private BaseQuickAdapter<ProblemBean, BaseViewHolder> mAdapter;
    private ProblemPagePresenter mPresenter;


    @Override
    protected String getContentTitle() {
        return getString(R.string.title_my_draft);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_draft;
    }

    @Override
    protected void loadDataByNet() {
        mPresenter = new ProblemPagePresenter(this);
        mList = new ArrayList<>();
        initRecyclerView();
        mPresenter.httpProblemsData("", "", 1, 0, 0, "null", "null", "", "", true);
    }

    @Override
    protected void onDataReloadByNet(View v) {
        mPresenter.httpProblemsData("", "", 1, 0, 0, "null", "null",  "", "",true);
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.httpProblemsData("", "", 1, 0, 0, "null", "null", "", "", true);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DraftAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, R.drawable.shape_my_draft_divider));
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

                String maxTime = mAdapter.getItem(mAdapter.getItemCount() - 2).getCreateTime();
                mPresenter.httpProblemsData(maxTime, "", 1, 0, 0, "null", "null",  "", "",false);


            }
        }, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ReportProblemActivity.startForResult(MyDraftActivity.this, true, mAdapter.getItem(position), position);
//            }
//        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.right) {
                    mPresenter.deleteDraft(mAdapter.getItem(position).getId(), position);
                } else if (view.getId() == R.id.content) {
                    ReportProblemActivity.startForResult(MyDraftActivity.this, true, mAdapter.getItem(position), position);
                }
            }
        });
//        mAdapter.setLoadMoreView(new CustomLoadMoreView());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == KEY_CODE_FOR_REQUEST) {
                boolean doCommitReport = data.getBooleanExtra(KEY_COMMIT_DRAFT, false);
                int position = data.getIntExtra(KEY_ITEM_POS, -1);
                if (position != -1) {
                    mAdapter.remove(position);
                    if (mAdapter.getItemCount() == 0) {
                        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
                    }
                }
                if (doCommitReport) {
                    mPresenter.httpMyDrafts();
                }

            }
        }
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(isRefresh);
        }
    }

    @Override
    public void setEnableLoadMore(boolean enableLoadMore) {
        mAdapter.setEnableLoadMore(enableLoadMore);
    }

    @Override
    public void onLoadMoreEnd(boolean isLoadMoreEnd) {
        mAdapter.loadMoreEnd(isLoadMoreEnd);
    }

    @Override
    public void onHttpProblemsDataSuccess(List<ProblemBean> datas, boolean isRefresh) {
        PostUtil.postSuccessDelayed(loadService);
        if (isRefresh) {
            mAdapter.setNewData(datas);
        } else {
            mAdapter.addData(datas);
        }
        mAdapter.loadMoreComplete();
    }

    @Override
    public void onHttpDraftsSuccess(List<ProblemBean> datas) {
        PostUtil.postSuccessDelayed(loadService);
        if (datas != null && datas.size() > 0) {
            mAdapter.addData(0, datas.get(0));
        }

    }


    @Override
    public void onError(ApiException e) {
        PostUtil.postCallbackDelayed(loadService, ErrorCallback.class);
    }


    @Override
    public void onNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }

    @Override
    public void onSearchNullData() {

    }

    @Override
    public void onFollowSuccess(long id, int position, ApiResult result) {
    }

    @Override
    public void onDeleteDraftSuccess(int position, ApiResult result) {
        if (result.getCode() == 0) {
            mAdapter.remove(position);
            if (mAdapter.getItemCount() == 0) {
                PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }
}
